package com.powernode.config;

import com.powernode.constant.QueueConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @create: 2024/7/26
 * @author: gyhe10
 * @description: 订单队列配置类
 **/

@Configuration
public class OrderQueueConfig {

    /**
     * 配置订单死信队列
     * @return
     */
    @Bean
    public Queue orderDeadQueue() {
        return new Queue(QueueConstants.ORDER_DEAD_QUEUE);
    }

    /**
     * 配置订单死信交换机
     * @return
     */
    @Bean
    public DirectExchange orderDeadExchange() {
        return new DirectExchange(QueueConstants.ORDER_DEAD_EXCHANGE);
    }

    /**
     * 配置订单延迟队列
     * @return
     */
    @Bean
    public Queue orderMsQueue() {
        // 配置延迟队列参数
        Map<String, Object> map = new HashMap<>();
        // 配置消息存活时长，单位毫秒，1分钟
        map.put("x-message-ttl", 1000 * 60);
        // 配置死信交换机
        map.put("x-dead-letter-exchange", QueueConstants.ORDER_DEAD_EXCHANGE);
        // 配置死信路由键
        map.put("x-dead-letter-routing-key", QueueConstants.ORDER_DEAD_ROUTING_KEY);

        return new Queue(QueueConstants.ORDER_MS_QUEUE, true, false, false, map);
    }

    /**
     * 配置订单延迟队列绑定关系
     * @return
     */
    @Bean
    public Binding orderDeadBinding() {
        return BindingBuilder.bind(orderDeadQueue()).to(orderDeadExchange()).with(QueueConstants.ORDER_DEAD_ROUTING_KEY);
    }
}
